package io.xiaojl.xj2ee.modules.sys.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import io.xiaojl.xj2ee.common.kit.PageWrapper;
import io.xiaojl.xj2ee.kit.JpaPageKit;
import io.xiaojl.xj2ee.modules.sys.entity.SysDeptEntity;
import io.xiaojl.xj2ee.modules.sys.entity.SysRoleEntity;
import io.xiaojl.xj2ee.modules.sys.repository.SysRoleRepository;
import io.xiaojl.xj2ee.modules.sys.service.SysDeptService;
import io.xiaojl.xj2ee.modules.sys.service.SysRoleDeptService;
import io.xiaojl.xj2ee.modules.sys.service.SysRoleMenuService;
import io.xiaojl.xj2ee.modules.sys.service.SysRoleService;
import io.xiaojl.xj2ee.modules.sys.service.SysUserRoleService;


@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {

	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	@Autowired
	private SysRoleDeptService sysRoleDeptService;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SysDeptService sysDeptService;
	@Autowired
	private SysRoleRepository sysRoleRepository;

	@Override
//	@DataFilter(subDept = true, user = false)
	public PageWrapper<SysRoleEntity> queryPage(Map<String, Object> params) {
		PageWrapper<SysRoleEntity> pw = new PageWrapper<SysRoleEntity>(params);
		Pageable pageable = JpaPageKit.getPageable(pw);
		Page<SysRoleEntity> page = sysRoleRepository.findAll(pageable);
		List<SysRoleEntity> list = page.getContent();
		for(SysRoleEntity sysRoleEntity : list){
			SysDeptEntity sysDeptEntity = sysDeptService.selectById(sysRoleEntity.getDeptId());
			if(sysDeptEntity != null){
				sysRoleEntity.setDeptName(sysDeptEntity.getName());
			}
		}
		
		pw.setList(list);
		pw.setTotalCount(Integer.parseInt(page.getTotalElements()+""));
		pw.setTotalPage(page.getTotalPages());
        return pw;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(SysRoleEntity role) {
		role.setCreateTime(new Date());
		sysRoleRepository.save(role);

		//保存角色与菜单关系
		sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());

		//保存角色与部门关系
		sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(SysRoleEntity role) {
		sysRoleRepository.saveAndFlush(role);

		//更新角色与菜单关系
		sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());

		//保存角色与部门关系
		sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteBatch(Long[] roleIds) {
		//删除角色
		List<SysRoleEntity> list = sysRoleRepository.findAllById(Arrays.asList(roleIds));
		sysRoleRepository.deleteInBatch(list);

		//删除角色与菜单关联
		sysRoleMenuService.deleteBatch(roleIds);

		//删除角色与部门关联
		sysRoleDeptService.deleteBatch(roleIds);

		//删除角色与用户关联
		sysUserRoleService.deleteBatch(roleIds);
	}

	@Override
	public List<SysRoleEntity> selectList(Map<String, Object> condition) {
		List<SysRoleEntity> list = sysRoleRepository.findAll(new Specification<SysRoleEntity>() {
			/** serialVersionUID*/
			private static final long serialVersionUID = 1L;
			@Override
			public Predicate toPredicate(Root<SysRoleEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//后续完善...
				return null;
			}
		});
		return list;
	}

	@Override
	public SysRoleEntity selectById(Long roleId) {
		return sysRoleRepository.getOne(roleId);
	}
}
